document.addEventListener('DOMContentLoaded', function() {
    // 加载动画
    setTimeout(function() {
        document.querySelector('.loader').classList.add('fade-out');
    }, 1500);
    
    // 图片懒加载
    const lazyLoadImages = function() {
        const imagePlaceholders = document.querySelectorAll('.image-placeholder');
        
        imagePlaceholders.forEach(placeholder => {
            const src = placeholder.getAttribute('data-src');
            if (!src) return;
            
            const img = new Image();
            img.src = src;
            img.onload = function() {
                placeholder.style.backgroundImage = `url(${src})`;
                placeholder.style.backgroundSize = 'cover';
                placeholder.style.backgroundPosition = 'center';
                placeholder.style.backgroundRepeat = 'no-repeat';
                placeholder.innerHTML = '';
            };
        });
    };
    
    // 导航栏滚动效果
    const navbar = document.querySelector('.navbar');
    window.addEventListener('scroll', function() {
        if (window.scrollY > 50) {
            navbar.style.background = 'rgba(255, 255, 255, 0.95)';
            navbar.style.boxShadow = '0 5px 20px rgba(0, 0, 0, 0.1)';
            navbar.style.padding = '15px 0';
        } else {
            navbar.style.background = 'transparent';
            navbar.style.boxShadow = 'none';
            navbar.style.padding = '20px 0';
        }
    });
    
    // 移动端菜单切换
    const burger = document.querySelector('.burger');
    const navLinks = document.querySelector('.nav-links');
    
    burger.addEventListener('click', function() {
        navLinks.classList.toggle('active');
        burger.classList.toggle('active');
    });
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            
            if (this.getAttribute('href') === '#') return;
            
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                window.scrollTo({
                    top: target.offsetTop - 80,
                    behavior: 'smooth'
                });
                
                // 关闭移动菜单
                if (navLinks.classList.contains('active')) {
                    navLinks.classList.remove('active');
                    burger.classList.remove('active');
                }
            }
        });
    });
    
    // 动画触发
    const animateOnScroll = function() {
        const elements = document.querySelectorAll('.project-card, .skill-item, .contact-method');
        
        elements.forEach(element => {
            const elementPosition = element.getBoundingClientRect().top;
            const screenPosition = window.innerHeight / 1.2;
            
            if (elementPosition < screenPosition) {
                element.style.opacity = '1';
                element.style.transform = 'translateY(0)';
            }
        });
    };
    
    // 初始化元素样式
    document.querySelectorAll('.project-card, .skill-item, .contact-method').forEach(element => {
        element.style.opacity = '0';
        element.style.transform = 'translateY(20px)';
        element.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
    });
    
    // 滚动监听
    window.addEventListener('scroll', animateOnScroll);
    
    // 初始加载
    lazyLoadImages();
    animateOnScroll();
    
    // 项目卡片悬停效果
    const projectCards = document.querySelectorAll('.project-card');
    projectCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            const content = this.querySelector('.project-content');
            content.style.transform = 'translateY(-10px)';
        });
        
        card.addEventListener('mouseleave', function() {
            const content = this.querySelector('.project-content');
            content.style.transform = 'translateY(0)';
        });
    });
});